﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoGo
{
    /// <summary>
    /// 游戏类
    /// </summary>
    class Game
    {
        //游戏区域的大小
        private const int GameWidth = 1000;
        private const int GameHeight = 700;
        //篮子大小
        private const int BasketWidth = 80;
        private const int BasketHeight = 50;
        //鸡蛋大小
        private const int EggWidth = 90;
        private const int EggHeight = 90;
        //落地打碎鸡蛋大小
        private const int BrokenEggWidth = 130;
        private const int BrokenEggHeight = 90;
        //落地鸡粪的大小
        private const int DroppedWidth = 130;
        private const int DroppedHeight = 90;
        //游戏提示目标分数图片大小
        private const int TipWidth = 500;
        private const int TipHeight = 70;
        //游戏当前得分图片大小
        private const int PresentScoreWidth = 250;
        private const int PresentScoreHeight = 50;
        //游戏倒计时条大小
        private const int CountWidth = 150;
        private const int CountHeight = 10;

        private Basket _basket;
        //定义一个集合，把鸡蛋装进去，即成员是egg
        private List<Egg> Eggs { get; set; }

        /// <summary>
        /// 游戏自带的Timer
        /// </summary>
        private System.Timers.Timer Timer { get; set; }

        //定义i，作为记录游戏时间是否结束的参数
        public int i = 0;
        //定义j，作为判断游戏的得分的参数
        public int j = 0;
        //定义k，作为判断游戏接到鸡蛋种类画good bad等提示语的参数
        private int k = 0;

        //定义委托
        public delegate void GameChanged();
        public delegate void GameOver();
        //定义事件
        public event GameChanged _gameChanged;
        public event GameOver _gameOver;

        //游戏开始时间
        private DateTime GameStartTime { get; set; }

        //下次产生鸡蛋时间
        private DateTime NextEggTime { get; set; }

        //初始化游戏得分
        private int Score { get; set; }

        //初始化篮子与鸡蛋碰撞的X坐标
        private int IntersectX { get; set; }

        /// <summary>
        /// 初始化游戏各类
        /// </summary>
        public Game()
        {
            //初始化最初分数
            Score = 0;
            //初始化产生鸡蛋时间
            NextEggTime = System.DateTime.Now.AddSeconds(1);
            //初始化篮子的位置坐标和大小
            _basket = new Basket(new Rectangle(455, 600, BasketWidth, BasketHeight));
            //初始化鸡蛋
            Eggs = new List<Egg>();
            //初始化游戏开始时间
            GameStartTime = System.DateTime.Now;
            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
        }

        /// <summary>
        /// 时钟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟处理程序
            foreach (var egg in Eggs)
            {
                egg.Move(new Size(GameWidth, GameHeight));
            }
            //随机产生鸡蛋
            if (System.DateTime.Now>NextEggTime)
            {
                Random random = new Random();
                int newX = 55 + random.Next(0, 5) * 200;
                int newType = random.Next(0, 100); 
                EggType eggType = EggType.junk;
                //产生百分之20的坏鸡蛋
                if (newType<20)
                {
                    eggType = EggType.spoiledEgg;
                }
                //产生百分之20的金鸡蛋
                else if (newType<40)
                {
                    eggType = EggType.goldEgg;
                }
                //产生百分之31的普通鸡蛋
                else if (newType < 71)
                {
                    eggType = EggType.normalEgg;
                }
                //产生百分之7的蓝鸡蛋，其余的产生鸡粪
                else if (newType < 78)
                {
                    eggType = EggType.blueEgg;
                }
                Egg egg = new Egg(new Rectangle(newX, 200, EggWidth, EggHeight), eggType);
                Eggs.Add(egg);
                //随机产生鸡蛋的间隔时间
                Random randomTime = new Random();
                //间隔设置在0-5秒之内
                int newTime = random.Next(0, 5000);
                NextEggTime = System.DateTime.Now.AddMilliseconds(newTime);
            }
            //处理碰撞
            HandleTouch();
            //处理输赢
            if ((System.DateTime.Now - GameStartTime).Seconds == 50)
            {
                //时间用完了，停止timer
                Timer.Stop();
                i = 1;
            }
            //通知FrmMain游戏改变了
            _gameChanged?.Invoke();
            //通知FrmMain游戏结束了
            _gameOver?.Invoke();
        }

        /// <summary>
        /// 处理碰撞
        /// </summary>
        private void HandleTouch()
        {
            //处理鸡蛋和篮子的碰撞，并记录加减分数
            Egg eggNow = null;
            foreach (var egg in Eggs)
            {
                if(egg.Rectangle.IntersectsWith(_basket.Rectangle))
                {
                    //处理得分，鸡粪扣10分
                    if (egg.EggType==EggType.junk)
                    {
                        Score -= 10;
                        k = 2;
                        IntersectX = egg.Rectangle.X;
                    }
                    //金鸡蛋加5分
                    else if (egg.EggType==EggType.goldEgg)
                    {
                        Score += 5;
                        k = 1;
                        IntersectX = egg.Rectangle.X;
                    }
                    //普通鸡蛋加2分
                    else if (egg.EggType == EggType.normalEgg)
                    {
                        Score += 2;
                    }
                    //蓝鸡蛋加3分
                    else if (egg.EggType == EggType.blueEgg)
                    {
                        Score += 3;
                    }
                    //坏鸡蛋加1分
                    else if (egg.EggType == EggType.spoiledEgg)
                    {
                        Score += 1;
                    }
                    eggNow = egg;//记下来
                    break;
                }
            }
            if(eggNow!=null)
            {
                Eggs.Remove(eggNow);
            }
            //处理鸡蛋和下边界碰撞
            Egg eggDrop = null;
            foreach (var egg in Eggs)
            {
                if((egg.Rectangle.Y+EggHeight)>650)
                {
                    eggDrop = egg;
                    //当落地的不是鸡粪的时候，记录k=3，和位置坐标X
                    if (egg.EggType!=EggType.junk)
                    {
                        k = 3;
                        IntersectX = egg.Rectangle.X;
                    }
                    break;
                }
            }
            if (eggDrop != null)
            {
                //清除落地鸡蛋
                Eggs.Remove(eggDrop);
            }
            //用j记录游戏最后得分
            j = Score;
        }

        /// <summary>
        /// 绘图游戏场景
        /// </summary>
        /// <param name="g"></param>
        /// <param name="size"></param>
        public void Draw(Graphics g,Size size)
        {
            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);
            //定义这张图的绘图句柄_g
            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
            //游戏背景
            _g.DrawImage(Properties.Resources.bg, rec);
            //画篮子
            _basket.Draw(_g);
            //画鸡蛋
            for (int i = 0; i < Eggs.Count; i++)
            {
                Egg egg = Eggs[i];
                //当鸡蛋的Y坐标大于200且小于550时绘图鸡蛋
                if (egg.Rectangle.Y > 200 && egg.Rectangle.Y < 550)
                {
                    egg.Draw(_g);
                }
            }
            //画打碎的鸡蛋
            for (int i = 0; i < Eggs.Count; i++)
            {
                Egg egg = Eggs[i];
                //鸡蛋或鸡粪落地，绘图打碎鸡蛋或落地的鸡粪
                if (egg.Rectangle.Y >= 550)
                {
                    //如果下落的是鸡粪，则画落地的鸡粪
                    if (egg.EggType == EggType.junk)
                    {
                        Rectangle rectangle = new Rectangle(egg.Rectangle.X - 20, egg.Rectangle.Y, DroppedWidth, DroppedHeight);
                        _g.DrawImage(Properties.Resources.dropped, rectangle);
                    }
                    //否则画打碎的鸡蛋
                    else
                    {
                        Rectangle rectangle = new Rectangle(egg.Rectangle.X - 20, egg.Rectangle.Y, BrokenEggWidth, BrokenEggHeight);
                        _g.DrawImage(Properties.Resources.brokenEgg, rectangle);
                    }
                }
            }
            //画游戏目标得分
            Rectangle recTip = new Rectangle(250, 0, TipWidth, TipHeight);
            _g.DrawImage(Properties.Resources.targetScore, recTip);
            //画游戏当前的分的提示语
            Rectangle recPresent = new Rectangle(10, 10, PresentScoreWidth, PresentScoreHeight);
            _g.DrawImage(Properties.Resources.presentScore, recPresent);
            //画游戏的得分
            Font drawFont = new Font("Arial", 30);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            _g.DrawString(Score.ToString(), drawFont, drawBrush, 150, 35);
            //画游戏接到金鸡蛋或者鸡粪或者落地的提示语
            for (int i = 0; i < Eggs.Count; i++)
            {
                Egg egg = Eggs[i];
                //当金鸡蛋与篮子碰撞时，画good
                if (k == 1)
                {
                    Font drawString = new Font("Arial", 30);
                    SolidBrush solidBrush = new SolidBrush(Color.FloralWhite);
                    _g.DrawString("Good", drawString, drawBrush, IntersectX, 500);
                    //将k的值归为零
                    k = 0;
                }
                //当鸡粪与篮子碰撞时，画bad
                else if (k==2)
                {
                    Font drawString = new Font("Arial", 30);
                    SolidBrush solidBrush = new SolidBrush(Color.FloralWhite);
                    _g.DrawString("Bad", drawString, drawBrush, IntersectX, 500);
                    //将k的值归为零
                    k = 0;
                }
                //当鸡蛋落地时，画miss
                else if (k == 3)
                {
                    Font drawString = new Font("Arial", 30);
                    SolidBrush solidBrush = new SolidBrush(Color.FloralWhite);
                    _g.DrawString("Miss", drawString, drawBrush, IntersectX, 500);
                    //将k的值归为零
                    k = 0;
                }
            }
            //画游戏倒计时
            //定义一个矩形，并绘制表示时间的图
            Rectangle recCount = new Rectangle(740, 10, 250, 50);
            _g.DrawImage(Properties.Resources.count, recCount);
            //游戏总时间为50秒，计算游戏剩余时间，并根据这个占比绘图玉米花蓝色矩形，表示所剩时间
            int cornflowerBlueWidth = CountWidth * (50 - (DateTime.Now - GameStartTime).Seconds) / 50;
            //当宽度小于零的时候，令宽度等于零
            if (cornflowerBlueWidth < 0)
            {
                cornflowerBlueWidth = 0;
            }
            //画游戏进度条
            Rectangle recCountGreen = new Rectangle(820, 40, cornflowerBlueWidth, CountHeight);
            _g.FillRectangle(new SolidBrush(Color.CornflowerBlue), recCountGreen);
            //窗口的缩放
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            _basket.KeyDown(key);
        }
    }
}
